//
//  HKCommonDefine.h
//  HKTimeKitApp
//
//  Created by huzhaoke1 on 2023/5/8.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

#define HKKeyWindow ([UIApplication sharedApplication].keyWindow)

#define HKScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define HKScreenHeight ([UIScreen mainScreen].bounds.size.height)

#define WEAK_REF(obj) __weak __typeof__(obj) weak_##obj = obj
#define STRONG_REF(obj) __strong __typeof__(weak_##obj) obj = weak_##obj

#define WS WEAK_REF(self);
#define SS \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
__strong __typeof__(weak_self) self = weak_self; \
_Pragma("clang diagnostic pop")


/**
 用 isKindOfClass 来判断类是否正确，正确返回实例，否则是 nil

 @param OBJ 被检查的实例
 @param CLASS 要匹配的类
 @return 匹配正确的话返回实例，否则是 nil
 */
#define KSSAFE_CAST(OBJ, CLASS)                                                \
        (                                                                      \
            {                                                                  \
                CLASS* result = nil;                                           \
                if ([OBJ isKindOfClass:[CLASS class]])                         \
                {                                                              \
                    result = (CLASS*)OBJ;                                      \
                }                                                              \
                result;                                                        \
            }                                                                  \
        )

#define KSSAFE_Protocol(OBJ, PROTOCOL)                                                \
(                                                                      \
    {                                                                  \
        id<PROTOCOL> result = nil;                                           \
        if ([OBJ conformsToProtocol:@protocol(PROTOCOL)])                         \
        {                                                              \
            result = (id<PROTOCOL>)OBJ;                                      \
        }                                                              \
        result;                                                        \
    }                                                                  \
)

// 颜色
#define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]

#define UIColorFromRGB(rgbValue) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
                green:((float)((rgbValue & 0x00FF00) >>  8))/255.0 \
                 blue:((float)((rgbValue & 0x0000FF) >>  0))/255.0 \
                alpha:1.0]

#define UIColorFromRGBA(rgbValue, alphaValue) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
                green:((float)((rgbValue & 0x00FF00) >>  8))/255.0 \
                 blue:((float)((rgbValue & 0x0000FF) >>  0))/255.0 \
                alpha:alphaValue]

@interface HKCommonDefine : NSObject

@end

NS_ASSUME_NONNULL_END
